package com.example.springboottest.controller;

import com.example.springboottest.entity.Article;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    private static final File articleDir;

    static {
        //注意空指针异常
        articleDir = new File("./articles");
        if (!articleDir.exists()) {
            boolean create  = articleDir.mkdirs();
            System.out.println(create);
        }

    }

    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理发表文章业务！");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println("标题:" + title + "\n作者:" + author + "\n内容:" + content);
        if (title == null || title.isEmpty() ||
                author == null || author.isEmpty() ||
                content == null || content.isEmpty()) {
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        Article article = new Article(title, author, content);
        File articleFile = new File(articleDir ,title + ".obj");
        if (articleFile.exists()) {
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        try (
                FileOutputStream fos = new FileOutputStream(articleFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos)
        ) {
            oos.writeObject(article);
            response.sendRedirect("/article_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/articleList")
    public void articleList(HttpServletResponse response) {
        System.out.println("开始处理文章列表业务！");
        File[] subs = articleDir.listFiles(pathname -> pathname.getName().endsWith(".obj"));

        List<Article> articleList = new ArrayList<>();
        assert subs != null;
        for (File sub : subs) {
            try (
                    FileInputStream fis = new FileInputStream(sub);
                    ObjectInputStream ois = new ObjectInputStream(fis)
            ) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println(articleList);

        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"2\" align=\"center\">");
            pw.println("<tr><td align=\"center\">标题</td><td align=\"center\">作者</td></tr>");
            for (Article article : articleList) {
                pw.println("<tr>");
                pw.println("<td>" + article.getTitle() + "</td>");
                pw.println("<td>" + article.getAuthor() + "</td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
